package doWork;

/*
    The KeyboardAnimation applet provides a generic framework for applets
    both display an animation and respond to keyboard events.  The animation
    runs only when the applet has the keyboard focus and can respond to 
    key presses.  The appearance of the applet changes, depending on whether
    it has the keyboard focus.  Note that each time a new frame of the 
    animation is to be displayed, it is drawn completely from scratch.
    When the applet has the keyboard focus, a cyan border is drawn around
    it.  When it does not have the keyboard focus, the border is in
    the applet's background color and a message "Click to activate" is
    displayed in the applet's foreground color and font.

    This class would be appropriate, for example, as a basis for a typical
    arcade game, such as Space Invaders.  (Except that the preformance
    won't be so good.)
    
    To use this framework, define a subclass of KeyboardAnimationApplet and
    override the drawFrame() method.  This method is responsible for drawing
    one frame of the animation.  If you need to some initialization at the
    time the applet is created, override the doInitialization() method.
    This method is called once when the applet is created.  (You should
    NOT override the standard applet methods, init(), start(), stop(), or
    destroy() unless you call the inherited versions from this class.  These
    routines perform important functions in this class.)
    
    In this class, the applet is already set up to "listen" for keyboard
    events.  To make your applet respond to keyboard events, you should
    override one or more of the methods keyPressed(), keyReleased(),
    and keyTyped().  (The applet also listens for MouseEvents, and you
    can override the mouse handling events if you want.  But if you do
    override mousePressed(), be sure to call requestFocus() in that
    routine.)
    
    To respond to key presses, you should have some instance variables
    that affect the image drawn.  Change these variables in the keyPressed,
    keyReleased, or keyTyped methods.
    
    (Alternatively, instead of defining a subclass, you could copy this
    file, change its name and the name of the class, and edit it.)
    
    (This applet is requires Java 1.1, since it uses Java 1.1 style event
    handling.)
    
    David Eck
    Department of Mathematics and Computer Science
    Hobart and William Smith Colleges
    Geneva, NY 14456
    eck@hws.edu
    
    October 2, 1998
    Small modifications 18 February 2000
*/

import java.awt.*;
import java.awt.event.*;

public class KeyboardAnimationApplet extends java.applet.Applet 
                 implements Runnable, KeyListener, MouseListener {
	String msg = "RUNNING STRING ";
	Thread t = null;
	Font fr, fc;
	Choice chBkColor;
	Choice chForColor;
	Color colors[] = { Color.WHITE, Color.BLACK, Color.BLUE, Color.GREEN,
			Color.RED };
	
   public void keyTyped(KeyEvent evt) { 

   }
   
   public void keyPressed(KeyEvent evt) { 
	   System.out.println("test");
   }
   
   public void keyReleased(KeyEvent evt) { 

   }
   private int width = -1;
   private int height = -1;
   
   Color focusBorderColor = Color.cyan;
   
   public void init() {
     
		System.out.print("init");
		fr = new Font("Dialog", Font.BOLD, 20);
		fc = new Font("Dialog", Font.BOLD, 10);
		chBkColor = new Choice();
		chBkColor.add("White");
		chBkColor.add("Black");
		chBkColor.add("Blue");
		chBkColor.add("Green");
		chBkColor.add("Red");
		chBkColor.setFont(fc);
		add(chBkColor);

		chForColor = new Choice();
		chForColor.add("White");
		chForColor.add("Black");
		chForColor.add("Blue");
		chForColor.add("Green");
		chForColor.add("Red");
		chForColor.setFont(fc);
		add(chForColor);

		

		setFont(fr);
		
      addKeyListener(this);
      addMouseListener(this);
      //doInitialization(getSize().width - 6, getSize().height - 6);
   }
      

   synchronized public void start() {
	   System.out.print("Start");
		t = new Thread(this);
		t.start();
   }
 
   
   synchronized public void paint(Graphics g) {
	   Color c = colors[chBkColor.getSelectedIndex()];
		setBackground(c);
		c = colors[chForColor.getSelectedIndex()];
		setForeground(c);
		g.drawString(msg, width, height);
   
   } // end paint
   
   public void run() {
	   //System.out.print("Run");
		char ch;
		for (;;) {
			try {
				System.out.println("test");
				repaint();
				Thread.sleep(100);
				ch = msg.charAt(0);
				msg = msg.substring(1, msg.length());
				msg += ch;
			} catch (InterruptedException e) {
			}
		}
   }
   
   public void mousePressed(MouseEvent evt) {
        // Request the input focus when the user clicks on
        // the applet.  (On most platforms, there is no need
        // for this.  However, Sun's own Java implementation
        // requires it.)
      requestFocus();
   }
   
   public void mouseEntered(MouseEvent evt) { }  // Required by the
   public void mouseExited(MouseEvent evt) { }   //    MouseListener
   public void mouseReleased(MouseEvent evt) { } //       interface.
   public void mouseClicked(MouseEvent evt) { }


} // end class AnimationApplet
